﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using ROrganizer.Client;
using ROrganizer.Client.Files;
using ROrganizer.UI.Models;

namespace ROrganizer.UI.Controllers
{
    internal class ItemDetailsFormController
    {
        private readonly ImdbEntry[] entries;

        public ItemDetailsFormController(ImdbEntry[] entries)
        {
            this.entries = entries;
        }

        public string Title
        {
            get { return this.entries[0].Info.Title; }
        }

        public string Rating
        {
            get
            {
                if (this.entries[0].Info.Rating == null)
                    return String.Empty;

                return String.Format("{0}.{1}", this.entries[0].Info.Rating / 10, this.entries[0].Info.Rating % 10); 
            }
        }

        public string Tagline
        {
            get { return this.entries[0].Info.Tagline; }
        }

        public string Plot
        {
            get { return this.entries[0].Info.Plot; }
        }

        public string Published
        {
            get 
            {
                if (!this.entries[0].Info.Published.HasValue)
                    return String.Empty;

                return this.entries[0].Info.Published.Value.ToString("dd MMMM yyyy"); 
            }
        }

        public string[] Genres
        {
            get { return this.entries[0].Info.Genres; }
        }

        public string[] Keywords
        {
            get { return this.entries[0].Info.Keywords; }
        }

        public FileEntry[] Files
        {
            get
            {
                var repository = new MetaRepository();
                var metas = repository.All();
                var query = from entry in this.entries
                            join meta in metas on entry.File equals meta.File.Path into tmetas
                            from tmeta in tmetas.DefaultIfEmpty()
                            select new FileEntry
                            {
                                Path = entry.File,
                                Name = Path.GetFileName(entry.File),
                                Size = FileEnumerator.GetFileSize(entry.File),
                                Resolution = Resolution.From(tmeta),
                                Runtime = tmeta != null ? tmeta.Info.Runtime : 0,
                            };

                return query.OrderBy(x => x.Name).ToArray();
            }
        }

        public void OpenFile(FileEntry entry)
        {
            Process.Start(entry.Path);
        }

        public class FileEntry
        {
            public string Path { get; set; }
            public string Name { get; set; }
            public long Size { get; set; }
            public Resolution Resolution { get; set; }
            public int Runtime { get; set; }
        }
    }
}
